package com.roncoo.education.course.service.auth.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Component;

import com.roncoo.education.common.core.base.Page;
import com.roncoo.education.common.core.base.PageUtil;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.core.tools.BeanUtil;
import com.roncoo.education.common.service.BaseBiz;
import com.roncoo.education.course.dao.ExperimentDao;
import com.roncoo.education.course.dao.UserExperimentDao;
import com.roncoo.education.course.dao.impl.mapper.entity.Experiment;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentExample;
import com.roncoo.education.course.dao.impl.mapper.entity.UserExperiment;
import com.roncoo.education.course.dao.impl.mapper.entity.UserExperimentExample;
import com.roncoo.education.course.service.auth.req.AuthUserExperimentReq;
import com.roncoo.education.course.service.auth.resp.AuthExperimentResp;
import com.roncoo.education.course.service.auth.resp.AuthUserExperimentResp;
import com.roncoo.education.course.service.biz.resp.ExperimentResp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;

/**
 * AUTH-课程用户关联表
 *
 * @author wujing
 */
@Component
@RequiredArgsConstructor
public class AuthUserExperimentBiz extends BaseBiz {

    @NotNull
    private final UserExperimentDao dao;
    @NotNull
    private final ExperimentDao experimentDao;

    /**
     * 实验管理列表
     * @param req
     * @param userId
     * @return
     */
    public Result<Page<AuthExperimentResp>> listGlForPage(AuthUserExperimentReq req, long userId) {
        ExperimentExample example = new ExperimentExample();
        example.createCriteria().andZuozheIdEqualTo(req.getLecturerId());
        example.setOrderByClause("gmt_modified desc");
        Page<Experiment> experimentPage = experimentDao.page(req.getPageCurrent(), req.getPageSize(), example);
        Page<AuthExperimentResp> respPage = PageUtil.transform(experimentPage, AuthExperimentResp.class);        
        return Result.success(respPage);
    }

    /**
     * 我的实验
     * 
     * @param req
     * @param userId
     * @return
     */
    public Result<Page<AuthUserExperimentResp>> listForPage(AuthUserExperimentReq req, long userId) {
        UserExperimentExample example = new UserExperimentExample();
        example.createCriteria().andUserIdEqualTo(userId);
        example.setOrderByClause("gmt_modified desc");
        Page<UserExperiment> userExperimentPage = dao.page(req.getPageCurrent(), req.getPageSize(), example);
        Page<AuthUserExperimentResp> respPage = PageUtil.transform(userExperimentPage, AuthUserExperimentResp.class);                
        
        if (CollUtil.isNotEmpty(respPage.getList())) {
            List<Long> experimentIdList = respPage.getList().stream().map(AuthUserExperimentResp::getExperimentId).collect(Collectors.toList());
            // 用户学习记录，获取每个课程里面最新学习的课时
            ExperimentExample example2 = new ExperimentExample();
            example2.createCriteria().andIdIn(experimentIdList);
            List<Experiment> experimentList = experimentDao.listByExample(example2);
            Map<Long, Experiment> experimentMap = new HashMap<>();
            if (CollUtil.isNotEmpty(experimentList)) {
            	experimentMap = experimentList.stream().collect(Collectors.toMap(item -> item.getId(), item -> item));
            }
            for (AuthUserExperimentResp resp : respPage.getList()) {
                Experiment experiment = experimentMap.get(resp.getExperimentId());
                if (ObjectUtil.isNotEmpty(experiment)) {
                	ExperimentResp experimentResp = BeanUtil.copyProperties(experiment, ExperimentResp.class);
                	resp.setExperiment(experimentResp);                	
                }
            }
        }
        return Result.success(respPage);
    }
}
